package com.example.hyznbk.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.example.hyznbk.R;
import com.example.hyznbk.presenter.SplashPresenter;
import com.example.hyznbk.utils.RxHelper;
import com.example.hyznbk.view.base.BaseActivity;
import com.example.hyznbk.view.view_interface.ISplashView;
import com.lancoo.cpbase.authentication.activities.LoginActivity;
import com.lancoo.cpbase.authentication.base.LoginOperate;
import com.lancoo.reviewsystem.utils.ToastUtils;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;

import rx.Subscriber;

/**
 * Created by Administrator on 2017/5/18.
 */
public class WelcomeActivity extends BaseActivity implements ISplashView {
    private SplashPresenter mMainPresenter;

    private final int SPLASH_WAIT_TIME = 2000;//默认欢迎页面停留的时间

    private long loginTime;
    private LoginOperate mOperate;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.SplashTheme);
        super.onCreate(savedInstanceState);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initViews() {
        loginTime = System.currentTimeMillis();
        mMainPresenter = new SplashPresenter(this);
        mOperate = new LoginOperate(WelcomeActivity.this);
        int result = mOperate.getCurrentUserState();
                if (result == 1) {

                    UserInfo.init();//初始化身份认证相关的数据
                    mMainPresenter.getData();
                }else if(result == 0){
                    //ToastUtils.showLong(getApplicationContext(),getString(R.string.no_user_message));
                    goLoginActivity();
                }else{
                   //ToastUtils.showLong(getString(R.string.network_error));
                   goLoginActivity();
                }

    }


    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    public void goMainActivity(boolean isSucceed) {
        if (!isSucceed){
            goLoginActivity();
        }else {

            long currTime = System.currentTimeMillis();
            RxHelper.countdown((int) ((currTime - loginTime + SPLASH_WAIT_TIME) / 1000))
                    .compose(this.<Integer>bindToLifecycle())
                    .subscribe(new Subscriber<Integer>() {
                        @Override
                        public void onCompleted() {
                            doSkip();
                        }

                        @Override
                        public void onError(Throwable e) {
                            ToastUtils.showLong(getString(R.string.network_error));
                            goLoginActivity();
                        }

                        @Override
                        public void onNext(Integer integer) {

                        }
                    });
        }

    }

    @Override
    public void goLoginActivity(){

        mOperate.logOut();
        startActivity(new Intent(getHolderActivity(),LoginActivity.class));
        finish();
    }

    private void doSkip(){
        startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
        overridePendingTransition(R.anim.hold, R.anim.zoom_in_exit);
        finish();
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        mOperate.stopService();
    }

    @Override
    public void onBackPressed() {
        //不响应后退键
        return;
    }
}